﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Luminoise
{
    public enum VoronoiType
    {
        Cell = 0,
        F1 = 1,
        F2 = 2,
        F3 = 3,
        F2F1 = 4,
    }

    public sealed class Voronoi : Module
    {
        static int instanceCount = 0;
        int instanceIndex = 0;

        public float Frequency { get; set; }
        public VoronoiType Type { get; set; }

        public override string FunctionName
        {
            get { return "cl_voronoi" + instanceIndex.ToString(); }
        }

        public override string GetCode()
        {
            return 
@"#ifndef " + FunctionName.ToUpper() + @"
#define " + FunctionName.ToUpper() + @"

float " + FunctionName + @"(__global int *p, Single3 input)
{
    float frequency = " + Frequency.ToString() + @";
    return " + GetVoronoiFunction() + @"(p, (float4)(input.x * frequency, input.y * frequency, input.z * frequency, 0));
}

#endif

";
        }

        private string GetVoronoiFunction()
        {
            switch (Type)
            {
                case VoronoiType.Cell:
                    return "voronoiCell";
                case VoronoiType.F1:
                    return "voronoiF1";
                case VoronoiType.F2:
                    return "voronoiF2";
                case VoronoiType.F3:
                    return "voronoiF3";
                case VoronoiType.F2F1:
                    return "voronoiF2F1";
                default:
                    throw new NotImplementedException();
            }
        }

        public Voronoi(float frequency, VoronoiType type)
        {
            Frequency = frequency;
            Type = type;

            instanceIndex = instanceCount;
            instanceCount++;
        }
    }
}
